import os
import cv2
import numpy as np

root = '/home/sean/data/MOT/MOT17Det/train'
imgpath = os.path.join(root, '%s', 'img1', '%s.jpg')
annotation = os.path.join(root, '%s', 'gt', 'gt.txt')

img_id = ('MOT17-02', '000001')
print(imgpath % img_id)
img = cv2.imread(imgpath % img_id)
height, width, _ = img.shape

target = []
gt_file = annotation % img_id[0]
for line in open(gt_file):
    frame_num, id, x, y, w, h, conf, cls, vis = line.split(',')
    # mm = int(img_id[1])
    if int(frame_num) == int(img_id[1]) and int(conf)==1 and float(vis)>0.3:
        print(conf, cls, vis)
        target.append([(int(x)-1)/width, (int(y)-1)/height, (int(x)+int(w)-1)/width, (int(y)+int(h)-1)/height, 0])  # [x_min, y_min, x_max, y_max, class(0 for object here)]
        x_min, y_min, x_max, y_max, _ = target[-1]
        x_min = x_min * width + 1
        y_min = y_min * height + 1
        x_max = x_max * width + 1
        y_max = y_max * height + 1
        img = cv2.rectangle(img, (int(x_min),int(y_min)), (int(x_max),int(y_max)), (255,0,0),2)
        cv2.imshow('test', img)
print(len(target))

cv2.waitKey(0)

# target = np.array(target)
# img, boxes, labels = self.transform(img, target[:, :4], target[:, 4])
# target = np.hstack((boxes, np.expand_dims(labels, axis=1)))

# to rgb
# img = img[:, :, (2, 1, 0)]

# maskroi = np.zeros([img.shape[0], img.shape[1]])
# for box in list(boxes):
#     box[0] *= img.shape[1]
#     box[1] *= img.shape[0]
#     box[2] *= img.shape[1]
#     box[3] *= img.shape[0]
#     pts = np.array([[box[0], box[1]], [box[2], box[1]], [box[2], box[3]], [box[0], box[3]]], np.int32)
#     maskroi = cv2.fillPoly(maskroi, [pts], 1)
# cv2.imshow('mask',cv2.resize(maskroi, (700,700)))
# cv2.waitKey(0)